package com.opgea.crm.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.opgea.crm.domain.entities.security.Group;
import com.opgea.crm.domain.entities.security.ResourceDetails;
import com.opgea.crm.domain.entities.security.Role;
import com.opgea.crm.domain.modal.JsonModelMap;
import com.opgea.crm.services.security.GroupService;
import com.opgea.crm.services.security.ResourceDetailsService;
import com.opgea.crm.services.security.RoleService;

@Controller
@RequestMapping(value="permission")
public class PermissionController {

	@Autowired
	RoleService roleService;
	@Autowired
	ResourceDetailsService resourceDetailsService;
	@Autowired
	GroupService groupService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String show(HttpServletRequest request){
		return "permission";
	}

	@RequestMapping(method=RequestMethod.GET, value="resourceListByRole")
	public @ResponseBody 
	Map<String, Object> resourceListByRole(@RequestParam(required=false, value="roleId")String roleId){
		if(roleId != null){
			return JsonModelMap.success().data(resourceDetailsService.findByRole(new Long(roleId)));
		}else{
			return JsonModelMap.success();
		}
	}
	
	
	@RequestMapping(method=RequestMethod.GET, value="roleListByGroup")
	public @ResponseBody 
	Map<String, Object> roleListByGroup(@RequestParam(required=false, value="groupId")String groupId,
										@RequestParam(required=false, value="onlyAssigned")String onlyAssigned){
		if(groupId != null){
			return JsonModelMap.success().data(roleService.findAllRolesByGroup(new Long(groupId), new Boolean(onlyAssigned)));
		}else{
			return JsonModelMap.success();
		}
		
		
	}	
	
	@RequestMapping(method=RequestMethod.GET, value="groupList")
	public @ResponseBody 
	Map<String, Object> groupList(){
		return JsonModelMap.success().data(groupService.findAllGroup());
	}	
	
	@RequestMapping(method=RequestMethod.POST, value="save")
	public @ResponseBody Map<String, Object> save(@RequestParam(required=true, value="resourceDetailsId") String resourceDetailsId,
												  @RequestParam(required=true, value="create") String create,
												  @RequestParam(required=true, value="read") String read,
												  @RequestParam(required=true, value="update") String update,
												  @RequestParam(required=true, value="remove") String remove){
		ResourceDetails rd = resourceDetailsService.find(new Long(resourceDetailsId));
		rd.setCreate(new Boolean(create));
		rd.setRead(new Boolean(read));
		rd.setUpdate(new Boolean(update));
		rd.setDelete(new Boolean(remove));
		resourceDetailsService.update(rd);
		return JsonModelMap.success().data("success");
	}
	
	@RequestMapping(method=RequestMethod.POST, value="createRole")
	public @ResponseBody 
	Map<String, Object> createRole(@RequestParam(required=false, value="roleName")String roleName){
		Role role = new Role();
		role.setName(roleName);
		roleService.create(role);
		return JsonModelMap.success().data(role.getId());
	}

	@RequestMapping(method=RequestMethod.POST, value="createGroup")
	public @ResponseBody 
	Map<String, Object> createGroup(@RequestParam(required=false, value="groupName")String groupName){
		Group group = new Group();
		group.setName(groupName);
		groupService.create(group);
		return JsonModelMap.success().data(group.getId());
	}	
	
	@RequestMapping(method=RequestMethod.POST, value="assignRole")
	public @ResponseBody
	Map<String, Object> assignRoleToGroup(@RequestParam(required=true, value="groupId")String groupId,
										  @RequestParam(required=true, value="roleId")String roleId){

		Group group = groupService.find(new Long(groupId));
		List<Role> roleList = group.getRoles();
		if(roleList.size() == 0 || roleList == null){
			roleList = new ArrayList<Role>();
		}
		
		roleList.clear();
		String[] roleIdArray = roleId.split(","); 
		for(String roleStr : roleIdArray){
			System.out.println("Role Id : "+roleStr);
			Role role = roleService.find(new Long(roleStr));
			if(roleList.contains(role)){
				//roleList.remove(role);
			}else{
				roleList.add(role);
			}
		}
		group.setRoles(roleList);
		groupService.update(group);
		
		System.out.println("Controller >> GroupId : "+group.getId() +", RoleList Size : "+roleList.size());
		return JsonModelMap.success().data("success");
	}
}
